package cc.miwuya.property.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.core.BaseController;
import cc.miwuya.property.domain.House;
import cc.miwuya.property.domain.Meter;
import cc.miwuya.property.service.HouseService;
import cc.miwuya.property.service.MeterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @Author 迷雾森林
 * @Date 2024-02-27 9:42
 * @Desc 楼房控制器
 */
@RestController
@RequestMapping("/property/house")
public class HouseController extends BaseController {

    @Resource
    HouseService houseService;
    @Resource
    MeterService meterService;
    /**
     * 添加楼房
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody House house) {
        if (house.getDeliveryStatus() == 1)
            house.setDeliveryTime(LocalDateTime.now());
        if (house.getDeliveryStatus() == 0){
            //下面处理添加房屋逻辑
            //根据房屋信息查询数据库是否存在
            House houseServiceOne = houseService.getOne(new QueryWrapper<House>()
                    .eq("residential_id", house.getResidentialId())
                    .eq("house_number", house.getHouseNumber())
                    .eq("unit_number", house.getUnitNumber())
                    .eq("room_number", house.getRoomNumber())
            );
            //存在则无法添加
            if (houseServiceOne!=null){
                return Result.error("当前房屋已存在，无法添加");
            }
            //不存在跳出判断进行添加
        }
        houseService.saveOrUpdate(house);
        Meter waterMeter = new Meter();
        waterMeter.setHomeId(house.getId());
        waterMeter.setResidentialId(house.getResidentialId());
        waterMeter.setType(1);
        Meter gasMeter = new Meter();
        gasMeter.setHomeId(house.getId());
        gasMeter.setResidentialId(house.getResidentialId());
        gasMeter.setType(2);
        Meter electricMeter = new Meter();
        electricMeter.setHomeId(house.getId());
        electricMeter.setResidentialId(house.getResidentialId());
        electricMeter.setType(3);
        meterService.saveBatch(Arrays.asList(waterMeter, gasMeter, electricMeter));
        return Result.success();
    }

    /**
     * 获取指定小区所有楼
     */
    @GetMapping("/houseNumberList/{residentialId}")
    public Result houseNumberList(@PathVariable Long residentialId) {
        return Result.success(houseService.getHouseNumberList(residentialId));
    }

    /**
     * 获取指定小区指定楼的所有单元
     */
    @GetMapping("/unitNumberList/{residentialId}/{houseNumber}")
    public Result unitNumberList(@PathVariable Long residentialId, @PathVariable Long houseNumber) {
        return Result.success(houseService.getUnitNumber(residentialId, houseNumber));
    }

    /**
     * 获取指定小区指定楼指定单元的所有房屋
     */
    @GetMapping("/roomNumberList/{residentialId}/{houseNumber}/{unitNumber}")
    public Result roomNumberList(@PathVariable Long residentialId, @PathVariable Long houseNumber, @PathVariable Long unitNumber) {
        return Result.success(houseService.getRoomNumberList(getPage(), residentialId, houseNumber, unitNumber));
    }

    /**
     * 获取所有楼（一般认为在添加楼房的地方进行使用）
     */
    @GetMapping("/houseList/{residentialId}")
    public Result houseList(@PathVariable Long residentialId) {
        //数据库结构一对多，一个residential对应多个楼，每个楼对应多个房间，我只需要楼，且不重复
        return Result.success(houseService.list(new QueryWrapper<House>().eq("residential_id", residentialId).select("DISTINCT house_number")));

        //return Result.success(houseService.list(new QueryWrapper<House>().eq("residential_id",residentialId)));
    }

    /**
     * 获取业主名下所有楼房
     */
    @GetMapping("/ownerHouseList/{ownerId}")
    public Result getOwnerHouseList(@PathVariable Long ownerId,Long residentialId) {
        return Result.success(houseService.getOwnerHouseList(getPage(),ownerId,residentialId));
    }

    /**
     * 获取房屋详细信息
     */
    @GetMapping("/houseInfo/{houseId}")
    public Result getHouseInfo(@PathVariable Long houseId) {
        return Result.success(houseService.getHouseInfoByHouseId(houseId));
    }

    /**
     * 获取指定小区全部房屋
     */
    @GetMapping("/getHouseAllByResidential/{residentialId}")
    public Result getHouseAllByResidential(@PathVariable Long residentialId){
        return Result.success(houseService.getHouseAllByResidential(getPage(),residentialId));
    }
    /**
     * 获取指定小区全部房屋
     */
    @GetMapping("/getHouseAllByHouse/{residentialId}/{houseId}")
    public Result getHouseAllByHouse(@PathVariable Long residentialId, @PathVariable Long houseId){
        return Result.success(houseService.getHouseAllByHouse(getPage(),residentialId,houseId));
    }
}
